package com.hunray.rent.entity;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "t_department")
public class Department implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer id;

	private String no;

	private String name;

	private String identityName;

	private String descr;

	private List<Role> roles;
	
	private List<User> users;

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "tg_department")
	@TableGenerator(name = "tg_department", table = "t_id", pkColumnName = "f_table", pkColumnValue = "t_department", valueColumnName = "f_id", initialValue = 1, allocationSize = 1)
	@Column(name = "f_id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "f_no", nullable = false, unique = true)
	@NotNull
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	@Column(name = "f_name", nullable = false, unique = true)
	@NotNull
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "f_identity_name", nullable = false, unique = true)
	@NotNull
	public String getIdentityName() {
		return identityName;
	}

	public void setIdentityName(String identityName) {
		this.identityName = identityName;
	}

	@Column(name = "f_descr")
	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "department")
	public List<Role> getRoles() {
		return roles;
	}
	
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "department")
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Department other = (Department) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Department [name=" + name + ", identityName=" + identityName
				+ "]";
	}

}
